﻿package com.quickdev.intelligenthome.content  {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.quickdev.intelligenthome.controls.Calendar;
	import com.quickdev.intelligenthome.net.data.EnergyRequest;
	import com.quickdev.intelligenthome.predef.AppPredef;
	
	public class EnergyTotalContent extends MovieClip {
		public var dataReady :Boolean = true;
		public var req :EnergyRequest;
		private var valueSpriteArr :Array = new Array();
		
		public static var UNIT_HEIGHT :uint = 30;
		public static var UNIT_WIDTH :uint = 30;
		public static var COLOR_ARR :Array = new Array();
		COLOR_ARR[0] = 0xF8EEA7;
		COLOR_ARR[1] = 0xFDFE1F;
		COLOR_ARR[2] = 0xFAAF3E;
		
		public static var ORGINAL_X :int = -17;
		public static var ORGINAL_Y :int = -117;
		public static var DEPTH :uint = 50;
		
		private var fromYear :uint = 0;
		private var fromMonth :uint = 0;
		private var fromDay :uint = 0;
		private var toYear :uint = 0;
		private var toMonth :uint = 0;
		private var toDay :uint = 0;
		
		public function EnergyTotalContent() {
		}
		
		private function setFromDate(year :uint, month :uint, day :uint) :void {
			fromYear = year;
			fromMonth = month;
			fromDay = day;
		}
		
		private function adjustY() :void {
			var i :uint = 0;
			if( req.queryType == AppPredef.ENERGY_TYPE_MONTH ) {
				for( ; i < 5; ++i ) {
					this["yLabel" + i].text = 5 * ( i + 1 ) * 30;
				}
			}
			else {
				for( ; i < 5; ++i ) {
					this["yLabel" + i].text = 5 * ( i + 1 );
				}
			}
		}
		
		private function adjustX() :void {
			var i :uint = 0;
			var index :uint;
			if( req.queryType == AppPredef.ENERGY_TYPE_MONTH ) {
				var startM :uint = parseInt( req.startDate.split("-")[1] );
				var endM :uint = parseInt( req.endDate.split("-")[1] );
				
				for(; i < 6; ++i) {
					index = startM + i
					
					this["xLabel" + i].text = index + "月";
				}
				for( ; i < 6; ++i ) {
					this["xLabel" + i].text = "";
				}
			}
			else {
				var startD :uint = parseInt( req.startDate.split("-")[2] );
				var endD :uint = parseInt( req.endDate.split("-")[2] );
				
				var step :uint = (endD - startD) / 5;
				if( step == 0 ) {
					step = 1;
				}
				for(; i < 6; ++i) {
					index = startD + i * step;

					this["xLabel" + i].text = index;
				}
				for( ; i < 6; ++i ) {
					this["xLabel" + i].text = "";
				}
			}
		}
		
		public function test() :void {
			trace("In Test");
			
			var testSprite1 :Sprite = getValueSprite( 2.5 * Math.random() );
			var testSprite2 :Sprite = getValueSprite( 2.5 * Math.random() );
			var testSprite3 :Sprite = getValueSprite( 2.5 * Math.random() );
			var testSprite4 :Sprite = getValueSprite( 2.5 * Math.random() );
			var testSprite5 :Sprite = getValueSprite( 2.5 * Math.random() );
			var testSprite6 :Sprite = getValueSprite( 2.5 * Math.random() );
			
			testSprite1.x = ORGINAL_X + DEPTH * 0;
			testSprite1.y = ORGINAL_Y - testSprite1.height;
			addChild( testSprite1 );
			
			testSprite2.x = ORGINAL_X + DEPTH * 1;
			testSprite2.y = ORGINAL_Y - testSprite2.height;
			addChild( testSprite2 );
			
			testSprite3.x = ORGINAL_X + DEPTH * 2;
			testSprite3.y = ORGINAL_Y - testSprite3.height;
			addChild( testSprite3 );
			
			testSprite4.x = ORGINAL_X + DEPTH * 3;
			testSprite4.y = ORGINAL_Y - testSprite4.height;
			addChild( testSprite4 );
			
			testSprite5.x = ORGINAL_X + DEPTH * 4;
			testSprite5.y = ORGINAL_Y - testSprite5.height;
			addChild( testSprite5 );
			
			testSprite6.x = ORGINAL_X + DEPTH * 5;
			testSprite6.y = ORGINAL_Y - testSprite6.height;
			addChild( testSprite6 );
			
			valueSpriteArr.push(testSprite1);
			valueSpriteArr.push(testSprite2);
			valueSpriteArr.push(testSprite3);
			valueSpriteArr.push(testSprite4);
			valueSpriteArr.push(testSprite5);
			valueSpriteArr.push(testSprite6);
		}
		
		public function getValueSprite( value :Number ) :Sprite {
			var sprite :Sprite = new Sprite();
			var n :uint = Math.floor( value );
			var i :int = n;
			var y :uint = 0;
			
			sprite.graphics.beginFill(COLOR_ARR[ i ]);
			var h :Number = UNIT_HEIGHT * (value - i);
			sprite.graphics.drawRect(0, y, UNIT_WIDTH, h);
			y += h;
			
			for(i = n - 1; i >= 0; --i) {
				sprite.graphics.beginFill(COLOR_ARR[ i ]);
				sprite.graphics.drawRect(0, y, UNIT_WIDTH, UNIT_HEIGHT);
				y += UNIT_HEIGHT;
			}
			
			sprite.graphics.endFill();
			
			return sprite;
		}
		
		public function clear() :void {
			for(var i :int = valueSpriteArr.length - 1; i >= 0; --i) {
				removeChild( valueSpriteArr[ i ] );
			}
			valueSpriteArr = new Array();
		}
		
		private function addRadioButtonListener() :void {
			timeRB1.addEventListener(MouseEvent.CLICK, onRB1Click);
			timeRB2.addEventListener(MouseEvent.CLICK, onRB2Click);
		}
		
		private function onRB1Click( event :MouseEvent ) :void {
			fromCalendar.dateType = Calendar.TYPE_DAY;
			toCalendar.dateType = Calendar.TYPE_DAY;
			
			fromDateBtn.dateLabel.text = fromCalendar.toString();
			toDateBtn.dateLabel.text = toCalendar.toString();
		}
		
		private function onRB2Click( event :MouseEvent ) :void {
			fromCalendar.dateType = Calendar.TYPE_MONTH;
			toCalendar.dateType = Calendar.TYPE_MONTH;
			
			fromDateBtn.dateLabel.text = fromCalendar.toString();
			toDateBtn.dateLabel.text = toCalendar.toString();
		}
	}
	
}
